package org.scohen.juploadr.app.carbon;

import org.eclipse.swt.internal.Callback;
import org.eclipse.swt.internal.carbon.HICommand;
import org.eclipse.swt.internal.carbon.OS;

import org.scohen.juploadr.Messages;
import org.scohen.juploadr.app.AboutDialog;
import org.scohen.juploadr.app.JUploadr;
import org.scohen.juploadr.prefs.ConfigurationDialog;

import org.eclipse.swt.widgets.Display;

public class CarbonMenuEventHandler {
    private static final int kHICommandPreferences = ('p' << 24) + ('r' << 16)
            + ('e' << 8) + 'f';

    private static final int kHICommandAbout = ('a' << 24) + ('b' << 16)
            + ('o' << 8) + 'u';

    private static final int kHICommandServices = ('s' << 24) + ('e' << 16)
            + ('r' << 8) + 'v';
    private static final int kHICommandQuit = ('q' << 24) + ('u' << 16)
            + ('i' << 8) + 't';

    private JUploadr uploader;

    public CarbonMenuEventHandler(JUploadr uploadr) {
        this.uploader = uploadr;
    }

    public void hookApplicationMenu(Display display, final String aboutName) {

        Object target = new Object() {
            int commandProc(int nextHandler, int theEvent, int userData) {
                if (OS.GetEventKind(theEvent) == OS.kEventProcessCommand) {
                    HICommand command = new HICommand();
                    OS.GetEventParameter(theEvent, OS.kEventParamDirectObject,
                                         OS.typeHICommand, null,
                                         HICommand.sizeof, null, command);
                    switch (command.commandID) {
                    case kHICommandPreferences:
                        return handlePreferences();
                    case kHICommandAbout:
                        return handleAbout();
                    case kHICommandQuit:
                        return handleQuit();
                    default:
                        break;
                    }
                }
                return OS.eventNotHandledErr;
            }

            int handleQuit() {
                uploader.quit();
                return OS.noErr;
            }

            int handlePreferences() {
                ConfigurationDialog setup = new ConfigurationDialog(
                        JUploadr.window);
                setup.setText(Messages
                        .getString("juploadr.ui.dialog.preferences")); //$NON-NLS-1$
                setup.open();
                return OS.noErr;
            }

            int handleAbout() {
                new AboutDialog();
                return OS.noErr;
            }
        };

        final Callback commandCallback = new Callback(target, "commandProc", 3); //$NON-NLS-1$
        int commandProc = commandCallback.getAddress();
        if (commandProc == 0) {
            commandCallback.dispose();
            return; // give up
        }

        // Install event handler for commands
        int[] mask = new int[] { OS.kEventClassCommand, OS.kEventProcessCommand };
        OS.InstallEventHandler(OS.GetApplicationEventTarget(), commandProc,
                               mask.length / 2, mask, 0, null);

        // create About ... menu command
        int[] outMenu = new int[1];
        short[] outIndex = new short[1];
        if (OS.GetIndMenuItemWithCommandID(0, kHICommandPreferences, 1,
                                           outMenu, outIndex) == OS.noErr
                && outMenu[0] != 0) {
            int menu = outMenu[0];

            int l = aboutName.length();
            char buffer[] = new char[l];
            aboutName.getChars(0, l, buffer, 0);
            int str = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault,
                                                      buffer, l);
            OS.InsertMenuItemTextWithCFString(menu, str, (short) 0, 0,
                                              kHICommandAbout);
            OS.CFRelease(str);

            // add separator between About & Preferences
            OS.InsertMenuItemTextWithCFString(menu, 0, (short) 1,
                                              OS.kMenuItemAttrSeparator, 0);

            // enable pref menu
            OS.EnableMenuCommand(menu, kHICommandPreferences);

            // disable services menu
            OS.DisableMenuCommand(menu, kHICommandServices);
        }

        // schedule disposal of callback object
        display.disposeExec(new Runnable() {
            public void run() {
                commandCallback.dispose();
            }
        });

    }
}
